/**
 * 2018年11月26日下午3:54:50
 * @author 谢进财
 */
package com.baihang.project.ds.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.baihang.framework.dao.BaseDao;
import com.baihang.framework.kit.StrKit;
import com.baihang.project.ds.model.FangYuanModel;
import com.baihang.project.ds.model.dbMap.FangYuanRowMapper;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Repository
public class FangYuanDao extends BaseDao {
	private static Logger logger = LoggerFactory.getLogger(FangYuanDao.class);
	@Autowired
	private FangYuanRowMapper rowMapper;
	public boolean batchUpdateFangYuanList(JSONArray jsonArray) {
		String sql = "update room set D_FloorArea=?,D_InnerArea=?,D_AssessmentArea=?,D_Price=?,D_Total=?,D_discountPrice=?,D_discountTotal=?,D_column=?,d_position=?,D_HouseModel=?,D_RoomType=? where D_ID=?";
		super.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
			public int getBatchSize() {
				return jsonArray.size();
				// 这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
			}

			public void setValues(PreparedStatement ps, int i) throws SQLException {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				ps.setDouble(1, jsonObject.getDouble("D_FloorArea"));
				ps.setDouble(2, jsonObject.getDouble("D_InnerArea"));
				ps.setDouble(3, jsonObject.getDouble("D_AssessmentArea"));
				ps.setDouble(4, jsonObject.getDouble("D_Price"));
				ps.setDouble(5, jsonObject.getDouble("D_Total"));
				ps.setDouble(6, jsonObject.getDouble("D_discountPrice"));
				ps.setDouble(7, jsonObject.getDouble("D_discountTotal"));		
				ps.setInt(8, jsonObject.getInt("D_column"));
				ps.setString(9, jsonObject.getString("d_position"));
				ps.setString(10, jsonObject.getString("D_HouseModel"));
				ps.setString(11, jsonObject.getString("D_RoomType"));
				ps.setInt(12, jsonObject.getInt("D_ID"));
			}
		});

		return true;
	}
	@Transactional
	public boolean batchInsertFangYuanList(JSONArray jsonArray,String banname,int nodeId,String nodeType,String nodeOrder) {
		String sql = "insert into room (D_ID,D_RoomNo,D_Unit,D_FloorArea,D_InnerArea,D_AssessmentArea,D_Price,D_Total,D_discountPrice,D_discountTotal,D_column,D_position,D_HouseModel,D_RoomType,D_BanName,RcType,BanID,D_Monitor,D_CollectionCount,E_CollectionCount) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		super.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
			public int getBatchSize() {
				return jsonArray.size();
				// 这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
			}

			public void setValues(PreparedStatement ps, int i) throws SQLException {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				if(StrKit.isNotEmpty(jsonObject.getString("D_ID"))) {
					ps.setInt(1, jsonObject.getInt("D_ID"));
				}else {
					ps.setInt(1, getID(nodeOrder,i));
				}
				ps.setString(2, jsonObject.getString("D_RoomNo"));
				ps.setString(3, jsonObject.getString("D_Unit"));
				ps.setDouble(4, jsonObject.getDouble("D_FloorArea"));
				ps.setDouble(5, jsonObject.getDouble("D_InnerArea"));
				ps.setDouble(6, jsonObject.getDouble("D_AssessmentArea"));
				ps.setDouble(7, jsonObject.getDouble("D_Price"));
				ps.setDouble(8, jsonObject.getDouble("D_Total"));
				ps.setDouble(9, jsonObject.getDouble("D_discountPrice"));
				ps.setDouble(10, jsonObject.getDouble("D_discountTotal"));		
				ps.setInt(11, jsonObject.getInt("D_column"));
				ps.setString(12, jsonObject.getString("d_position"));
				ps.setString(13, jsonObject.getString("D_HouseModel"));
				ps.setString(14, jsonObject.getString("D_RoomType"));
				//ps.setString(15, jsonObject.getString("D_BanName"));
				ps.setString(15, banname);
				ps.setString(16, nodeType);//认筹类型
				ps.setInt(17, nodeId);//认筹类型
				ps.setString(18, jsonObject.getString("D_Monitor"));
				ps.setInt(19, jsonObject.getInt("D_CollectionCount"));
				ps.setInt(20, jsonObject.getInt("E_CollectionCount"));
			}
		});

		return true;
	}
	public List<FangYuanModel> queryForFangYuanList(String sql){
		logger.info(sql);
		List<FangYuanModel>  modelList = null;
		try {
			modelList = super.getJdbcTemplate().query(sql, rowMapper);
		}catch (Exception e) {
			logger.info("查询词源模型失败");
			logger.info(e.getMessage());
		}
		return modelList;
	}
	
	public FangYuanModel queryForFangYuan(String sql) {
		FangYuanModel model =null;
		try {
			model = super.getJdbcTemplate().queryForObject(sql, rowMapper);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return model;
	}
	 /**
	  * 拼出像1001的id号。后三位自增
	  * @param loudongID
	  * @param order
	  * @return
	  */
	 private Integer getID(String loudongOrder , int order) {
			// 得到一个NumberFormat的实例
			NumberFormat nf = NumberFormat.getInstance();
			// 设置是否使用分组
			nf.setGroupingUsed(false);
			// 设置最大整数位数
			nf.setMaximumIntegerDigits(3);
			// 设置最小整数位数
			nf.setMinimumIntegerDigits(3);
			// 输出测试语句
			String newID = String.format("%s%s", loudongOrder,nf.format(order));
			return Integer.valueOf(newID);
	 }
	
	
}
